﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using sistem;

namespace sistem
{
    public partial class AdministratorKabinetiKontrola : UserControl
    {
        List<Kabinet> kabineti = new List<Kabinet>();
        List<Kabinet> kabinetiReserve = new List<Kabinet>();

        public AdministratorKabinetiKontrola()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DodavanjeKabinetaForma noviKabinet = new DodavanjeKabinetaForma();
            noviKabinet.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            {
                UredjivanjeKabinetaForma urediKabinet = new UredjivanjeKabinetaForma((Kabinet)listBox1.SelectedItem);
                urediKabinet.Show();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                try
                {
                    Kabinet kab = new Kabinet();
                    kab = (Kabinet)listBox1.SelectedItem;
                    DAL.DAL d = DAL.DAL.Instanca;
                    d.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    d.getDAO.getKabinetDAO().delete(kab);
                    d.terminirajKonekciju();
                    kabineti.RemoveAt(listBox1.SelectedIndex);
                    d.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    kabineti = d.getDAO.getKabinetDAO().getAll();
                    d.terminirajKonekciju();
                }
                catch (Exception)
                {
                    MessageBox.Show("Došlo je do pogreške prilikom rada s bazom podataka", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (toolStripTextBox1.Text == "")
                {
                    listBox1.DataSource = kabinetiReserve;
                    listBox1.DataSource = kabineti;
                }
                else
                {
                    kabinetiReserve.Clear();
                    for (int i = 0; i < kabineti.Count; i++)
                    {
                        if (kabineti[i].ToString().ToUpper().Contains(toolStripTextBox1.Text.ToUpper()))
                            kabinetiReserve.Add(kabineti[i]);
                    }
                    listBox1.DataSource = kabineti;
                    listBox1.DataSource = kabinetiReserve;
                }
            }
        }

        private void AdministratorPredmetiKontrola_Load(object sender, EventArgs e)
        {
            try
            {
                DAL.DAL d = DAL.DAL.Instanca;
                d.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                kabineti = d.getDAO.getKabinetDAO().getAll();
                d.terminirajKonekciju();
            }
            catch (Exception)
            {
                MessageBox.Show("Došlo je do pogreške prilikom rada s bazom podataka", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            listBox1.DataSource = kabineti;
        }

        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (toolStripTextBox1.Text == "")
            {
                listBox1.DataSource = kabinetiReserve;
                listBox1.DataSource = kabineti;
            }
            else
            {
                kabinetiReserve.Clear();
                for (int i = 0; i < kabineti.Count; i++)
                {
                    if (kabineti[i].ToString().ToUpper().Contains(toolStripTextBox1.Text.ToUpper()))
                        kabinetiReserve.Add(kabineti[i]);
                }
                listBox1.DataSource = kabineti;
                listBox1.DataSource = kabinetiReserve;
            }
        }

        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                Kabinet kab = new Kabinet();
                kab = (Kabinet)listBox1.SelectedItem;
                label4.Text = kab.Naziv;
                label5.Text = kab.Sprat.ToString();
                label6.Text = kab.Kapacitet.ToString();
            }
        }

        public void FormReload()
        {
            try
            {
                DAL.DAL d = DAL.DAL.Instanca;
                d.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                kabineti = d.getDAO.getKabinetDAO().getAll();
                d.terminirajKonekciju();
            }
            catch (Exception)
            {
                MessageBox.Show("Došlo je do pogreške prilikom rada s bazom podataka", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            toolStripTextBox1.Text = "";
            kabinetiReserve.Clear();
            listBox1.DataSource = kabinetiReserve;
            listBox1.DataSource = kabineti;
        }
    }
}
